package cn.itcast.controller.company;

import cn.itcast.base.BaseController;
import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.utils.commons.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

@Controller
@RequestMapping(value = "/company")
public class CompanyController extends BaseController{

    @Autowired
    private CompanyService companyService;

    /*@RequestMapping(value = "/list", name = "查询所有企业")
    public String findAll() {

        List<Company> list = companyService.findAll();
        request.setAttribute("list", list);
        return "company/company-list";
    }*/
    @RequestMapping(value = "/list",name = "分页插件查询")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize){
        //分页查询企业数据
        PageInfo pb = companyService.pageHelperFindAll(pageNum,pageSize);
        request.setAttribute("page",pb);
        return "company/company-list";

    }

@RequestMapping(value = "/toUpdate",name = "企业页面修改")
    public String toUpdate(String id) {
    Company company = companyService.findCompanyById(id);
    request.setAttribute("company",company);
    return "company/company-update";
    }

    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id) {
        companyService.deleteCompanyById(id);
        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/toAdd", name = "企业添加页面")
    public String toAdd() {
        return "company/company-add";
    }


    @RequestMapping(value = "/edit",name = "企业信息添加/修改")
    public String edit(Company company) {

        if (UtilFuns.isEmpty(company.getId())) {
            //true为空
            String uuid = UUID.randomUUID().toString().replace("-", "");
            company.setId(uuid);
            // 调用service进行保存
            companyService.save(company);
        } else {
            //修改
            companyService.updateCompany(company);
        }
        return "redirect:/company/list.do";
    }
}
